package com.in.mobilerechargedth;


import java.util.List;

import com.in.mobilerechargedth.adapter.FirstScreenListViewAdapter;
import com.in.mobilerechargedth.api.FirstOperatorApi;
import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.FirstScreenModel;


import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FirstScreenActivity extends BaseActivity {

    ListView listOperator = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first_screen);
		listOperator = (ListView) findViewById(R.id.lvOperator);
		setupAdmob();
		share();
		new OperatorTask().execute(new String[] {});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);

	}


    /**
     * Async Task to proceed Operator.
     */
    private class OperatorTask extends AsyncTask<String, Void, List<FirstScreenModel>> {

        private ProgressDialog mProgress;


        @Override
        protected void onPreExecute() {
            mProgress = new ProgressDialog(FirstScreenActivity.this);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgress.setMessage("Loading...");
            mProgress.setCanceledOnTouchOutside(false);
            // Check press back key when showing dialog.
            mProgress.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
            mProgress.show();

            super.onPreExecute();
        }

        @Override
        protected List<FirstScreenModel> doInBackground(String... params) {
            FirstOperatorApi api = new FirstOperatorApi();
            List<FirstScreenModel> data = null;
            try {
                data = api.getOperator();
            } catch (Exception e) {
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<FirstScreenModel> result) {
            super.onPostExecute(result);

            if (result != null) {
                final FirstScreenListViewAdapter adapter = new FirstScreenListViewAdapter(FirstScreenActivity.this, result);
                listOperator.setAdapter(adapter);

                listOperator.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long id) {
                        long operatorId = adapter.getItemId(position);
                        Log.e("aaaaaaaaaaaaaaaaaaa", "operatorId: " + operatorId);
                        Intent i = new Intent(FirstScreenActivity.this,
                                SecondScreenActivity.class);
                        i.putExtra(ConstantVariables.OPERATOR_ID, operatorId);
                        startActivity(i);
                        // TODO Auto-generated method stub

                    }
                });
            }
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }

        }

    }

}
